/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entityprojet;

import entityprojet.exceptions.IllegalOrphanException;
import entityprojet.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author T.Ron
 */
public class TypectJpaController implements Serializable {

    public TypectJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }

    public TypectJpaController() {
    }
    
    
    
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("projetT_RPU");

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Typect typect) {
        if (typect.getComptetCollection() == null) {
            typect.setComptetCollection(new ArrayList<Comptet>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Comptet> attachedComptetCollection = new ArrayList<Comptet>();
            for (Comptet comptetCollectionComptetToAttach : typect.getComptetCollection()) {
                comptetCollectionComptetToAttach = em.getReference(comptetCollectionComptetToAttach.getClass(), comptetCollectionComptetToAttach.getIdCt());
                attachedComptetCollection.add(comptetCollectionComptetToAttach);
            }
            typect.setComptetCollection(attachedComptetCollection);
            em.persist(typect);
            for (Comptet comptetCollectionComptet : typect.getComptetCollection()) {
                Typect oldTypectOfComptetCollectionComptet = comptetCollectionComptet.getTypect();
                comptetCollectionComptet.setTypect(typect);
                comptetCollectionComptet = em.merge(comptetCollectionComptet);
                if (oldTypectOfComptetCollectionComptet != null) {
                    oldTypectOfComptetCollectionComptet.getComptetCollection().remove(comptetCollectionComptet);
                    oldTypectOfComptetCollectionComptet = em.merge(oldTypectOfComptetCollectionComptet);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Typect typect) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Typect persistentTypect = em.find(Typect.class, typect.getNumtypect());
            Collection<Comptet> comptetCollectionOld = persistentTypect.getComptetCollection();
            Collection<Comptet> comptetCollectionNew = typect.getComptetCollection();
            List<String> illegalOrphanMessages = null;
            for (Comptet comptetCollectionOldComptet : comptetCollectionOld) {
                if (!comptetCollectionNew.contains(comptetCollectionOldComptet)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Comptet " + comptetCollectionOldComptet + " since its typect field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Comptet> attachedComptetCollectionNew = new ArrayList<Comptet>();
            for (Comptet comptetCollectionNewComptetToAttach : comptetCollectionNew) {
                comptetCollectionNewComptetToAttach = em.getReference(comptetCollectionNewComptetToAttach.getClass(), comptetCollectionNewComptetToAttach.getIdCt());
                attachedComptetCollectionNew.add(comptetCollectionNewComptetToAttach);
            }
            comptetCollectionNew = attachedComptetCollectionNew;
            typect.setComptetCollection(comptetCollectionNew);
            typect = em.merge(typect);
            for (Comptet comptetCollectionNewComptet : comptetCollectionNew) {
                if (!comptetCollectionOld.contains(comptetCollectionNewComptet)) {
                    Typect oldTypectOfComptetCollectionNewComptet = comptetCollectionNewComptet.getTypect();
                    comptetCollectionNewComptet.setTypect(typect);
                    comptetCollectionNewComptet = em.merge(comptetCollectionNewComptet);
                    if (oldTypectOfComptetCollectionNewComptet != null && !oldTypectOfComptetCollectionNewComptet.equals(typect)) {
                        oldTypectOfComptetCollectionNewComptet.getComptetCollection().remove(comptetCollectionNewComptet);
                        oldTypectOfComptetCollectionNewComptet = em.merge(oldTypectOfComptetCollectionNewComptet);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = typect.getNumtypect();
                if (findTypect(id) == null) {
                    throw new NonexistentEntityException("The typect with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Typect typect;
            try {
                typect = em.getReference(Typect.class, id);
                typect.getNumtypect();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The typect with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Comptet> comptetCollectionOrphanCheck = typect.getComptetCollection();
            for (Comptet comptetCollectionOrphanCheckComptet : comptetCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Typect (" + typect + ") cannot be destroyed since the Comptet " + comptetCollectionOrphanCheckComptet + " in its comptetCollection field has a non-nullable typect field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(typect);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Typect> findTypectEntities() {
        return findTypectEntities(true, -1, -1);
    }

    public List<Typect> findTypectEntities(int maxResults, int firstResult) {
        return findTypectEntities(false, maxResults, firstResult);
    }

    private List<Typect> findTypectEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Typect.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Typect findTypect(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Typect.class, id);
        } finally {
            em.close();
        }
    }

    public int getTypectCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Typect> rt = cq.from(Typect.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
